<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends Model {

	public $id;
	public $group;
	public $rayon;
	public $name;
	public $package;

	public $offset;
	public $limit;
	public $order_by;
	public $record_count;
	public $join;

	function __construct()
	{
		parent::__construct();
		$this->table = 'product';
	}

	function get()
	{
		($this->id) ? $this->db->where('id', $this->id) : NULL ;
		($this->group) ? $this->db->where('group', $this->group) : NULL ;
		($this->rayon) ? $this->db->where('rayon', $this->rayon) : NULL ;
		($this->name) ? $this->db->where('name', $this->name) : NULL ;
		($this->package) ? $this->db->where('package', $this->package) : NULL ;

		($this->limit AND !$this->offset) ? $this->db->limit($this->limit) : NULL ;
		($this->limit AND $this->offset) ? $this->db->limit($this->limit, $this->offset) : NULL ;
		($this->order_by) ? $this->db->order_by($this->order_by) : NULL ;

		if(is_array($this->join))
		{
			foreach($this->join as $join_table => $fields)
			{
				$join_type = (array_key_exists(2, $fields)) ? $fields[2] : 'LEFT' ;
				$this->db->join($join_table, $this->table.'.'.$fields[0] .'='.$join_table.'.'.$fields[1], $join_type);
			}
		}

		$query = $this->db->get($this->table);

		$this->record_count = $query->num_rows();

		if($query->num_rows() > 0)
		{
			if($this->id)
			{
				return $query->row();
			}
			else
			{
				return $query->result();
			}
		}
		else
		{
			return FALSE;
		}
	}
	
	function search($search){
		($this->id) ? $this->db->where('id', $this->id) : NULL ;
		($this->group) ? $this->db->where('group', $this->group) : NULL ;
		($this->rayon) ? $this->db->where('rayon', $this->rayon) : NULL ;
		($search) ? $this->db->like('name', $search) : NULL ;
		($this->package) ? $this->db->where('package', $this->package) : NULL ;

		($this->limit AND !$this->offset) ? $this->db->limit($this->limit) : NULL ;
		($this->limit AND $this->offset) ? $this->db->limit($this->limit, $this->offset) : NULL ;
		($this->order_by) ? $this->db->order_by($this->order_by) : NULL ;

		if(is_array($this->join))
		{
			foreach($this->join as $join_table => $fields)
			{
				$join_type = (array_key_exists(2, $fields)) ? $fields[2] : 'LEFT' ;
				$this->db->join($join_table, $this->table.'.'.$fields[0] .'='.$join_table.'.'.$fields[1], $join_type);
			}
		}

		$query = $this->db->get($this->table);

		$this->record_count = $query->num_rows();

		if($query->num_rows() > 0)
		{
			if($this->id)
			{
				return $query->row();
			}
			else
			{
				return $query->result();
			}
		}
		else
		{
			return FALSE;
		}
	
	}

	function save()
	{
		$data = array();
		($this->id) ? $data['id'] = $this->id : NULL ;
		($this->group) ? $data['group'] = $this->group : NULL ;
		($this->rayon) ? $data['rayon'] = $this->rayon : NULL ;
		($this->name) ? $data['name'] = $this->name : NULL ;
		($this->package) ? $data['package'] = $this->package : NULL ;

		($this->id) ? $this->db->where('id', $this->id)->update($this->table, $data) : NULL ;
		(!$this->id) ? $this->db->where('id', $this->id)->insert($this->table, $data) : NULL ;
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE ; 
	}

	function delete()
	{
		($this->id) ? $this->db->where('id', $this->id)->delete($this->table) : NULL ;
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE ; 
	}

	function count_recs()
	{
		return ($this->get()) ? count($this->get()) : 0 ; 
	}
}

/* End of file product.php */
/* Location: ./system/application/models/product.php */